home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / visulztn / sphinx / sphinx.lha / utils / build_cm.c next >
Encoding:
C/C++ Source or Header  |  1993-03-27  |  3.0 KB  |  139 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. /*
  4.         Build a Sphinx Color-map
  5.  
  6.   Ce programme construit un fichier color-map au format de sphinx a partir des
  7. donnees lues sur stdin. Le nom du fichier color-map  a  construire  doit  etre
  8. passe en parametre. Le format des donnees sur stdin est :
  9.  
  10. b i red green blue
  11.  
  12.     b     : nom du banc (r : red, g : green, b : blue)
  13.     i     : numero d'entree (de 0 a 255)
  14.     red   : niveau de rouge (de 0 a 255)
  15.     green : niveau de vert  (de 0 a 255)
  16.     blue  : niveau de bleu  (de 0 a 255)
  17.  
  18. Les niveaux non definis sont mis a zero.
  19.  
  20. */
  21.  
  22. struct color_map
  23.     {
  24.     short red   [256];
  25.     short green [256];
  26.     short blue  [256];
  27.     };
  28.  
  29. typedef struct color_map color_map;
  30.  
  31. main(argc,argv)
  32.     int argc;
  33.     char * argv [];
  34. {
  35.     int fd_out;
  36.     color_map red_map, green_map, blue_map;
  37.  
  38.     /* Le nom du fichier color-map doit etre passe en argument */
  39.     if (argc != 2)
  40.     {
  41.     fprintf(stderr,"Usage is %s color-map-file\n",argv[0]);
  42.     exit(1);
  43.     }
  44.  
  45.     /* Ouverture du fichier color-map */
  46.     fd_out = open(argv[1],O_WRONLY | O_CREAT);
  47.     if (fd_out < 0)
  48.     {
  49.     fprintf(stderr,"Cannot write %s\n",argv[1]);
  50.     exit(1);
  51.     }
  52.  
  53.     /* Creation de la color map a partir de stdin */
  54.     create_map(&red_map,&green_map,&blue_map);
  55.  
  56.     /* Ecriture du fichier color-map */
  57.     write_map(fd_out,&blue_map);
  58.     write_map(fd_out,&green_map);
  59.     write_map(fd_out,&red_map);
  60.  
  61.     exit(0);
  62. }
  63.  
  64. /* Creation de la color map a partir de stdin */
  65.  
  66. create_map(red_map,green_map,blue_map)
  67.     color_map * red_map, * green_map, * blue_map;
  68. {
  69.     int i, n, red, green, blue;
  70.     int line_number;
  71.     char bank;
  72.     color_map * map;
  73.     char buffer [128];
  74.  
  75.     clear_map(red_map);
  76.     clear_map(green_map);
  77.     clear_map(blue_map);
  78.  
  79.     line_number = 0;
  80.     while (gets(buffer) != NULL)
  81.     {
  82.         n = sscanf(buffer,"%c %d %d %d %d",
  83.                &bank,&i,&red,&green,&blue);
  84.         if ((n != 5) || out_of_range(i) ||
  85.             out_of_range(red) || out_of_range(green) || out_of_range(blue))
  86.         error(line_number);
  87.         line_number++;
  88.     switch(bank)
  89.         {
  90.         case 'r' : map = red_map;   break;
  91.         case 'g' : map = green_map; break;
  92.         case 'b' : map = blue_map;  break;
  93.             default  : error(line_number);
  94.         }
  95.     map->red [i]   = red;
  96.     map->green [i] = green;
  97.     map->blue [i]  = blue;
  98.     }
  99. }
  100.  
  101. out_of_range(val)
  102.     int val;
  103. {
  104.     return((val < 0) || (val > 255));
  105. }
  106.  
  107. error(line_number)
  108.     int line_number;
  109. {
  110.     fprintf(stderr,"Bad input format at line number %d\n",line_number);
  111.     exit(1);
  112. }
  113.  
  114. clear_map(map)
  115.     color_map * map;
  116. {
  117.     int i;
  118.     for (i = 0; i < 256; i++)
  119.     map->red [i]   =
  120.     map->green [i] =
  121.     map->blue [i]  = 0;
  122. }
  123.  
  124. write_map(fd_out,map,name)
  125.     int fd_out;
  126.     color_map * map;
  127.     char * name;
  128. {
  129.     int n;
  130.     n = write(fd_out,map->red,sizeof(map->red));
  131.     if (n != (sizeof(map->red))) return(-1);
  132.     n = write(fd_out,map->green,sizeof(map->green));
  133.     if (n != (sizeof(map->green))) return(-1);
  134.     n = write(fd_out,map->blue,sizeof(map->blue));
  135.     if (n != (sizeof(map->blue))) return(-1);
  136.     return(0);
  137. }
  138.  
  139.